package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.qy.dao.VoucherDao;
import java.util.List;
import java.util.Map;
import java.sql.Types;
import com.qy.db.DbParameter;
import java.util.HashMap;
import java.util.ArrayList;
import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.Voucher;
import com.qy.db.BaseDao;

/**
* s_voucher daoImpl实现类的生成
* @author zhangwenlong
* @date Tue Jan 30 12:11:31 GMT+08:00 2018
*/ 
public class VoucherDaoImpl extends BaseDao  implements VoucherDao {

	@Override
	public boolean createVoucher(Voucher voucher) {
		boolean result = false;
		int id = -1;
		if (voucher.beginAdd()) {
			String sql = "insert into s_voucher (id,voucher_price,sell_price,name,v_desc,image) values (?,?,?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, voucher.getId()));
			param.put(2, new DbParameter(Types.INTEGER, voucher.getVoucherPrice()));
			param.put(3, new DbParameter(Types.INTEGER, voucher.getSellPrice()));
			param.put(4, new DbParameter(Types.VARCHAR, voucher.getName()));
			param.put(5, new DbParameter(Types.VARCHAR, voucher.getVDesc()));
			param.put(6, new DbParameter(Types.VARCHAR, voucher.getImage()));
			id = execLastId(sql, param);
			result = id > -1;
			voucher.commitAdd(result);
			voucher.setId(id);
		}
		return result;
	}

	@Override
	public List<Voucher> getVoucherList() {
		String sql = "select * from s_voucher;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<Voucher> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<Voucher>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public Voucher getVoucherById(int id) {
		String sql = "select * from s_voucher where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		Voucher voucher = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					voucher = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return voucher;
	}

	@Override
	public boolean updateVoucher(Voucher voucher) {
		boolean result = false;
		if (voucher.beginUpdate()) {
			String sql = "update s_voucher set voucher_price=?,sell_price=?,name=?,v_desc=?,image=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, voucher.getVoucherPrice()));
			param.put(2, new DbParameter(Types.INTEGER, voucher.getSellPrice()));
			param.put(3, new DbParameter(Types.VARCHAR, voucher.getName()));
			param.put(4, new DbParameter(Types.VARCHAR, voucher.getVDesc()));
			param.put(5, new DbParameter(Types.VARCHAR, voucher.getImage()));
			param.put(6, new DbParameter(Types.INTEGER, voucher.getId()));
			result = execNoneQuery(sql, param) > -1;
			voucher.commitUpdate(result);
		}
		return result;
	}

	public Voucher resultTo(ResultSet rs) throws SQLException {
		Voucher voucher = new Voucher();
		voucher.setId(rs.getInt("id"));
		voucher.setVoucherPrice(rs.getInt("voucher_price"));
		voucher.setSellPrice(rs.getInt("sell_price"));
		voucher.setName(rs.getString("name"));
		voucher.setVDesc(rs.getString("v_desc"));
		voucher.setImage(rs.getString("image"));
		voucher.setOp(Option.None);
		return voucher;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}

}

